package py.edu.ucom.cadira.logic;

import java.util.ArrayList;
import java.util.List;
import org.jdom.Element;
import py.edu.ucom.cadira.game.war.Board;
import py.edu.ucom.cadira.game.war.GameInterface;
import py.edu.ucom.cadira.game.war.WarGame;
import py.edu.ucom.cadira.game.war.WarUser;
import py.edu.ucom.cadira.net.User;
import py.edu.ucom.cadira.net.XMLSerializable;

/**
 * Lugar en el que dos usuarios juegan, pueden exisitr varios watchers
 * observando el estado
 * 
 * @author danicricco
 * 
 */
public class Mesa {
    private Integer mesaId;
    private GameInterface gameInterface;
    private User player1;
    private User player2;
    private ArrayList<User> watchers;
    private boolean status;
    private static final String NODE_PLAYERS = "usuario";
    private static final String NODE_WATCHERS = "watchers";

    public Mesa() {
        this.status = false;
        this.watchers = new ArrayList<User>();
        this.setGameInterface(new WarGame());
    }
    public Mesa(Integer mesaId) {
        this.status = false;
        this.watchers = new ArrayList<User>();
        this.mesaId = mesaId;
        this.setGameInterface(new WarGame());
        this.gameInterface.setMesaId(mesaId);
    }
    public GameInterface getGameInterface() {
        return gameInterface;
    }
    public void setGameInterface(GameInterface gameInterface) {
        this.gameInterface = gameInterface;
    }
    public void setPlayer1(User player1) {
        this.player1 = player1;
        this.gameInterface.userRegistration(player1,1);
    }
    public WarUser getWarUser(User user){
        return this.gameInterface.getWarUser(user);
    }
    public void setPlayer2(User player2) {
        this.player2 = player2;
        this.gameInterface.userRegistration(player2,1);
    }
    public void setWatchers(ArrayList<User> watchers) {
        this.watchers = watchers;
    }
    public Integer getMesaId() {
        return mesaId;
    }
    public void setMesaId(Integer tableId) {
        this.mesaId = tableId;
    }
    public User getPlayer1() {
        return player1;
    }
    public User getPlayer2() {
        return player2;
    }
    public boolean isStatus() {
        return status;
    }
    public void setStatus(boolean status) {
        this.status = status;
    }
    public void delPlayer (User player){
        if (player1 == player) {
            player1 = null;
            this.gameInterface.unregisterUser(getWarUser(player));
        } else if (player2 == player) {
            player2 = null;
            this.gameInterface.unregisterUser(getWarUser(player));
        }
        if (player1 != null && player2 != null) {
            this.status = true;
        }else
            this.status = false;        
    }
    private boolean checkRegister(User player){
        if (this.player1 != player && this.player2 != player)
            return true;
        return false;
    }
    public WarUser addPlayer(User player) {
            WarUser warUser = null;
            if (player1 == null && checkRegister(player)) {
                player1 = player;
                warUser = this.gameInterface.userRegistration(player,0);
            } else if (player2 == null && checkRegister(player)) {
                player2 = player;
                warUser = this.gameInterface.userRegistration(player,0);
            }
            if (player1 != null && player2 != null) {
                this.status = true;
            }
            return warUser;
    }
    
    public ArrayList<User> getWatchers() {
        return watchers;
    }
    public void setWatchers(User usuario) {
        this.watchers.add(usuario);
        this.gameInterface.userRegistration(usuario,1);
    }
    public Board getBoard(User user) {
        Board board;
        if (user == player1 || user == player2) {
            // significa que el usuario es un player
            board = gameInterface.getBoard(gameInterface.getWarUser(user));
        } else {
            board = gameInterface.getBoard();
        }
        return board;
    }
    public Board getBoard(Object obj) {
        if (obj instanceof XMLSerializable) {
            return gameInterface.getBoard();
        }
        return null;
    }
    public boolean getStatusBoard() {
        return gameInterface.getStatusBoard();
    }
    public void setStatusBoard(boolean status){
        this.gameInterface.setStatusGame(status,this);
    }
    public void toXml(Element tableNode) {
        tableNode.setAttribute("id", getMesaId().toString());
        Element statusNode = new Element("status");
        statusNode.addContent(String.valueOf(isStatus()));
        tableNode.addContent(statusNode);
        if (getPlayer1() != null) {
            Element userNode1 = getWarUser(getPlayer1()).toXml();
            tableNode.addContent(userNode1);
        }
        if (getPlayer2() != null) {
            Element userNode2 = getWarUser(getPlayer2()).toXml();
            tableNode.addContent(userNode2);
        }
        Element watcherNode = new Element(NODE_WATCHERS);
        for (User mesasWatcher : this.watchers) {
            watcherNode.addContent(mesasWatcher.toXml());
            tableNode.addContent(watcherNode);
        }
        
    }
    @SuppressWarnings("unchecked")
    public Mesa fromXml(Element tableNode) {
        List<Element> userNode = null;
        List<Element> watcherNode = null;
        User user = null;
        WarUser warUser = null;
            this.setMesaId(Integer.parseInt(tableNode.getAttributeValue("id")));
            this.setStatus(Boolean.valueOf(tableNode.getChild("status").getValue()));
            userNode = tableNode.getChildren(NODE_PLAYERS);
            for (Element nodeUsers : userNode) {
                warUser = new WarUser();
                this.addPlayer(warUser.fromXml(nodeUsers));
            }
            Element nodeWatcher = tableNode.getChild(NODE_WATCHERS);
            if (nodeWatcher != null){
                watcherNode = nodeWatcher.getChildren(NODE_PLAYERS);
                for (Element nodeWatchers : watcherNode) {
                    user = new User();
                    this.setWatchers(user.fromXml(nodeWatchers));
                }
            }
        return this;
    }
}
